<?php
namespace TbfDocument;
//未使用
class TbfDocument {
    static function FormProcess($config,$hookKey,$obj = array()){
        $c = new Context();
        $c->config = $config;
        $c->obj = $obj;
        $addConfig = array();
        foreach($c->config['Field'] as $field){
            if (empty($field[$hookKey])){
                $addConfig[] = FieldProcessor::FormString($c,$field['Name']);
                continue;
            }
            $thisConfig = self::callProcessorFunc($c,$field['Name'],'Form',$field[$hookKey]);
            if (empty($thisConfig)){
                continue;
            }
            if (!empty($field['Need'])){
                $thisConfig['Need'] = true;
            }
            $addConfig[] = $thisConfig;
        }
        return \TbfFormView::generate($addConfig);
    }
    static function ActionProcess($config,$hookKey){
        $c = new Context();
        $c->config = $config;
        foreach($c->config['Field'] as $field){
            $value = \TbfInput::inStr($field['Name']);
            if (!empty($field['Need'])){
                if (empty($value)){
                    \TbfError::client(sprintf('请输入[%s]',$field['ShowName']));
                    return null;
                }
            }
            $c->obj[$field['Name']] = $value;
            if (empty($field[$hookKey])){
                FieldProcessor::ActionString($c,$field['Name']);
                continue;
            }
            self::callProcessorFunc($c,$field['Name'],'Action',$field[$hookKey]);
        }
        return $c->obj;
    }

    //多个processor的类
    static $processorClassNameList = array('TbfDocument\FieldProcessor');
    private static function callProcessorFunc(Context $c,$fieldName,$type,$config){
        if (!is_array($config)){
            $config = array($config);
        }
        $funcName = $config[0];
        $args = $config;
        array_shift($args);
        $methodName = $type.$funcName;
        $cb = null;
        foreach(self::$processorClassNameList as $className){
            if (method_exists($className,$methodName)){
                $cb = array($className,$methodName);
            }
        }
        if (empty($cb)){
            \TbfError::unExpect('[UeeDocumentField.callFunc] method['.$methodName.'] not found');
        }
        $args = array_merge(array($c,$fieldName),$args);
        return call_user_func_array($cb,$args);
    }
}
